package com.nguyen.database;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {

	private Context mContext;
	private static String DB_PATH = "/data/data/com.nguyen.letcook/databases/";
	private static String DATABASE_NAME = "LetCook.sqlite3";
	private SQLiteDatabase mDB;
	
	/**
	 * Constructor
	 * @param context
	 */
	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, 1);
		mContext = context;
		if(!checkDataBase()){
			this.getReadableDatabase();
			try{
				copyDatabase();
			}
			catch(Exception ex){
				ex.printStackTrace();
			}
		}
	}

	@Override
	public void onCreate(SQLiteDatabase arg0) {
		

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
	}
	
	/**
	 * check if database is existed
	 * @return
	 */
	private boolean checkDataBase(){
		SQLiteDatabase checkDB = null;

		try{
			String myPath = DB_PATH + DATABASE_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

		}catch(SQLiteException e){
		//database doesn't exist yet.
		}
		if(checkDB != null){
			checkDB.close();
		}
		return checkDB != null ? true : false;
	}

	/**
	 * copy database to device
	 * @throws IOException
	 */
	private void copyDatabase() throws IOException{
		//Open your local db as the input stream
		InputStream myInput = mContext.getAssets().open(DATABASE_NAME);
		// Path to the just created empty db
		//String outFileName = DB_PATH + DATABASE_NAME;	
		String outFileName = DB_PATH + DATABASE_NAME;
		//Open the empty db as the output stream
		OutputStream myOutput = new FileOutputStream(outFileName);

		//transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;

		while ((length = myInput.read(buffer))>0){
		myOutput.write(buffer, 0, length);
		}
		//Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();

	}
	
	/**
	 * delete an item.
	 * tableName: the name of table in database
	 * id : id of item need to delete
	 * @param tableName
	 * @param id
	 */
	public void delete(String tableName, int id){
		openDataBase();
		String query = "DELETE FROM "+ tableName +" WHERE id = '"+ id + "'";
		mDB.execSQL(query);		
		close();
	}
	
	/**
	 * open database
	 * @throws SQLException
	 */
	public void openDataBase() throws SQLException{
		//Open the database
		String myPath = DB_PATH + DATABASE_NAME;
		mDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
	}

	/**
	 * Close database
	 */
    @Override
	public synchronized void close() { 
	    if(mDB != null)
		    mDB.close(); 
	    super.close(); 
	}
    
    
    /**
     * get number of row of table
     * @param tableName
     * @param condition
     * condition : 1 | 0
     * @return
     */
    public int getTableRows(String tableName, int condition){
    	int count = 0;
    	
    	String sql = "SELECT COUNT(id) FROM " + tableName;
    	if(condition != -1)
    		sql+= " WHERE isnew = "+condition;
    	Cursor cursor = mDB.rawQuery(sql, null);
    	if(cursor!= null){
    		cursor.moveToFirst();
    		count = cursor.getInt(0);
    	}    	
    	cursor.close();
    	return count;
    }
    
    public boolean writeDishToDB(){
    	boolean ret = true;
    	
    	return ret;
    }
}
